package com.tannm.doan.taskmanager.agenda;

import java.util.ArrayList;

import com.tannm.doan.taskmanager.TaskManager;
import com.tannm.doan.taskmanager.dataview.DataView;
import com.tannm.doan.taskmanager.dataview.DataViewItem;
import com.tannm.doan.taskmanager.views.*;

import android.graphics.Paint;
import android.widget.LinearLayout;

public class AgendaViewDay extends AgendaView {
	// fields
	private ArrayList<LinearLayout> vecHourItems = new ArrayList<LinearLayout>();

	// appointment click listener
	public ViewDayHourItem.OnItemClick onNewApptItemClick = new ViewDayHourItem.OnItemClick() {
		public void OnClick(ViewDayHourItem item) {
			doHourOfDayClick(item, ViewTodayItemHeader.ViewType.Appointments,
					item.getHour(), item.getMinutes());
		}
	};

	// fields
	private int iTextHeight = 0;
	private int iHourWidth = 0;
	private int iTimeWidth = 0;
	private int iUSTimeMarkWidth = 0;
	private int iSpaceWidthTime = 0;
	private int iSpaceWidthMinutes = 0;
	private int iSpaceWidthUSTimeMark = 0;

	// fields
	private Paint mpt = new Paint();

	// methods
	public AgendaViewDay(TaskManager main) {
		super(main);
		iTextHeight = ViewDayHourItem.getTextHeight(mpt);
		iHourWidth = ViewDayHourItem.getSpaceWidthHour(mpt);
		iTimeWidth = ViewDayHourItem.getSpaceWidthTime(mpt);
		iUSTimeMarkWidth = ViewDayHourItem.getSpaceWidthUSTimeMark(mpt);
		iSpaceWidthTime = ViewTodayItemAppointment.getSpaceWidthTime(mpt);
		iSpaceWidthMinutes = ViewTodayItemAppointment.getSpaceWidthMinutes(mpt);
		iSpaceWidthUSTimeMark = ViewTodayItemAppointment
				.getSpaceWidthUSTimeMark(mpt);
	}

	@Override
	public int getViewType() {
		return viewMode.DAY;
	}

	@Override
	public int getViewIndex() {
		return 1;
	}

	@Override
	public void rebuild() {
		llayParent.removeAllViews();

		vecHourItems.clear();

		// build 24 hour day view
		for (int iHour = 0; iHour < 24; iHour++) {
			ViewDayHourItem item = new ViewDayHourItem(main, iHour, iTextHeight);
			item.setItemData(main.prefs.b24HourMode, iHourWidth, iTimeWidth,
					iUSTimeMarkWidth);
			llayParent.addView(item);
			vecHourItems.add(iHour, item);
		}
	}

	public void removeChildViewsFromAllHourItems() {
		for (int iHour = 0; iHour < vecHourItems.size(); iHour++) {
			LinearLayout item = vecHourItems.get(iHour);
			if (item != null)
				item.removeAllViewsInLayout();
		}
		llayParent.invalidate();
	}

	@Override
	public void rebuildViewAppointments(DataView dataView) {
		final boolean bIsViewToday = isViewToday();
		final int iCurrentHour = getTodayCurrentHour();

		removeChildViewsFromAllHourItems();

		// update time format
		for (int iHour = 0; iHour < vecHourItems.size(); iHour++) {
			ViewDayHourItem item = (ViewDayHourItem) vecHourItems.get(iHour);
			item.setItemData(main.prefs.b24HourMode, iHourWidth, iTimeWidth,
					iUSTimeMarkWidth);
			item.setCurrentHour((bIsViewToday) ? iCurrentHour : -1);
			item.requestLayout();
		}

		// rebuild view
		for (int i = 0; i < dataView.getRowsCountTotal(); i++) {
			DataViewItem row = dataView.getRow(i, this.getViewType());
			if (row != null) {
				int iHour = row.getStartHour();
				ViewTodayItemAppointment item = new ViewTodayItemAppointment(
						main);
				item.setRowId(row.lID);
				item.setItemTime(row.getStartHour(), row.getStartMinute(),
						true, main.prefs.b24HourMode, iSpaceWidthTime,
						iSpaceWidthMinutes, iSpaceWidthUSTimeMark);
				item.setItemData(row.sSubject, row.bAlarm, row.isRepeat());
				item.setItemClick(onApptItemClick);

				ViewDayHourItem hourItem = (ViewDayHourItem) vecHourItems
						.get(iHour);
				if (hourItem != null) {
					hourItem.addView(item, lparams);
				}
			}
		}

		// update heights
		for (int iHour = 0; iHour < 24; iHour++) {
			ViewDayHourItem item = (ViewDayHourItem) vecHourItems.get(iHour);
			item.updateHeight();
			item.setItemClick(onNewApptItemClick);
		}
	}

	@Override
	public void rebuildViewNotes(DataView dataView) {
	}

	@Override
	public void rebuildViewTasks(DataView dataView) {
	}

	@Override
	public void updateTimeFormat() {
		for (int iHour = 0; iHour < vecHourItems.size(); iHour++) {
			ViewDayHourItem item = (ViewDayHourItem) vecHourItems.get(iHour);
			if (item != null)
				item.updateTimeFormat(main.prefs.b24HourMode, iHourWidth,
						iTimeWidth, iUSTimeMarkWidth);
		}
	}

}
